<?php

namespace Modules\Goods\Http\Controllers;


use App\User;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Modules\Goods\Entities\Attr;
use Modules\Goods\Entities\Fenlei;
use Modules\Goods\Entities\Goods;
use Modules\Goods\Entities\Shop;
use Modules\Goods\Service\HomeService;
use Auth;
class HomeController extends Controller
{

//    public function __construct()
//    {
//        $this->middleware('auth');
//    }


    /**
     * 前台首页
     *
     * Display a listing of the resource.
     * @return Response
     */
    public function index()
    {
       //new这个HomeService对象，调用里面的getfenlei()方法
        $fenlei = (new HomeService())->getFenlei(0);
//        dd($fenlei);
        return view('goods::home.index',compact('fenlei'));
    }


    //加载前台注册模板方法
    public function regist(){
        return view('goods::home.regist');
    }
    //处理前台用户注册
    public function register(Request $request){
//        dd($request->all());
        $post = User::create([
                'name' => $request->input('name'),
                'email' => $request->input('email'),
                'password' => bcrypt($request->input('password'))
        ]);
//        session()->flash('success','注册成功');
//        $post->save();
        return redirect()->route('logins');

    }

    //加载前台登录模板方法
    public function login(){
        return view('goods::home.login');
    }
    //处理前台登录
    public function logincl(Request $request){
        $post = Auth::attempt([
            'email' => $request->input('email'),
            'password' => $request->input('password')
        ]);
        if ($post){
            return redirect('/');
        }
        session()->flash('danger','用户名或密码输入错误');
        return back();
    }

    //处理前台登录的post数据提交方法
//    public function logincl(Request $request){
////        "$2y$10$mZR3oBnSs4YAkzMRQRZjIOYQJ.Og6vCQMJRFbJVBPl/as2WNQy/W6";
////        dd($request->all())->toArray();
//        //获取当前用户的登录数据
//        $data = $request->all();
//        //获取当前登录用户的账号
//        $zhanghao = $data['email'];
//        //获取当前登录用户的密码
//        $mima = $data['password'];
////        dd($mima);
//        //获取前台用户表里所有的账号数据
////        $userzhangh = User::where('email','=',$zhanghao)->get();
//
//        $aa = \DB::table('users')->where('email','=',$zhanghao)->get()->toArray();
////        dd($aa);
//        foreach ($aa as $v){
//            $xx = \Hash::check($mima,$v['password']);
//            dd($xx);
//        }

////        $userzhanghao = User::pluck('email')->toArray();
//////        dd($adminuser);
////        //获取前台用户表里所有的密码数据
//        $usermima = User::pluck('password')->toArray();
//////        dd($usermima);
////
////        //进行判断，如果用户登录的用户名和密码跟用户表里面的某一个用户名及密码相同，那么就代表当前登录的用户存在，就允许登录，如果有一项不相同，就不允许登录
////        if (in_array($zhanghao,$userzhanghao)){
////            dd('1');
////        }else{
////            dd('2');
////        }
//
//
//    }


    /**
     * 前台列表页
     *
     * @param $cid
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function lists($cid){

        $fenlei=(new HomeService())->getListsGoods($cid);
        //获取$cid对应分类页商品的数据
        $fenlei=$fenlei[0];
        $goods = (new HomeService())->getListsGoods($cid)[1];
//        dd($goods);
        //获取$cid对应分类页商品的属性数据
        $listsAttr = (new HomeService())->getListsAttrs($cid);
//        dd($listsAttr);
        return view('goods::home.list',compact('goods','listsAttr','fenlei'));
    }

    /**
     * 前台商品详情页
     *
     * @param $gid
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function xiangqing($gid){
        //找到对应$sid的商品数据分配展示
        $goods = Goods::find($gid);
//        dd($goods);
        //然后获取当前商品的可用属性
        $keyongAttrs = Attr::whereIn('id',explode(',',$goods['attrs']))->get()->toArray();
//        dd($keyongAttrs);
        //找到当前商品的所有货品
        $huopin = Shop::where('goods_id',$gid)->pluck('attrs')->toArray();
//        dd($huopin);
        //将当前商品的所有货品的属性组合变成数组
        $huopin = implode(',',$huopin);
//        dd($huopin);
        //然后将数组去重,array_unique()方法内传的值必须是一个字符串
        $huopin = array_unique(explode(',',$huopin));
//        dd($huopin);
        //循环当前商品的可用属性
        foreach($keyongAttrs as $k=>$v){
            $keyongAttrs[$k]['son'] = Attr::where('pid',$v['id'])->whereIn('aname',$huopin)->get()->toArray();
        }
//        dd($keyongAttrs);

        return view('goods::home.xiangqing',compact('goods','keyongAttrs'));
    }


    public function getHuopin($gid,$aids){
        //找到对应商品的货品数据
        $huopin = Shop::where('goods_id',$gid)->where('attrs',$aids)->first();
        //判断,如果找得到货品数据,将货品数据返回,如果找不到货品数据,返回数据中告诉前台没找到货品数据
        if ($huopin){
            return ['valid' => 1,'data' => $huopin];
        }else{
            return ['valid' => 0,'data' => []];
        }
    }


    //加载个人信息模板
    public function geren(){
        return view('goods::home.gerenxinxi');
    }
    //加载修改账户信息的模板
    public function zhanghu_anq(){
        return view('goods::home.zhanghu_anq');
    }

}
